<template>
  <div class="box">
    <!-- 查询 -->
    <el-form
      :inline="true"
      size="mini"
      :model="searchData"
      class="demo-form-inline"
    >
      <!-- 订单号 -->
      <el-form-item label="订单号">
        <el-input v-model="searchData.orderNo" placeholder="订单号"></el-input>
      </el-form-item>
      <!-- 收货人 -->
      <el-form-item label="收货人">
        <el-input
          v-model="searchData.consignee"
          placeholder="收货人"
        ></el-input>
      </el-form-item>
      <!-- 手机号 -->
      <el-form-item label="手机号">
        <el-input v-model="searchData.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <!-- 订单状态 -->
      <el-form-item label="订单状态">
        <el-select v-model="searchData.orderState" placeholder="订单状态">
          <el-option label="未受理" value="未受理"></el-option>
          <el-option label="已受理" value="已受理"></el-option>
          <el-option label="派送中" value="派送中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
      <!-- 选择时间 -->

      <el-form-item label="选择时间">
        <el-date-picker
          v-model="searchData.date"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table :data="tableData" border :style="{ width: w + 'px' }">
      <!-- 订单号 -->
      <el-table-column fixed prop="orderNo" label="订单号" width="150">
      </el-table-column>
      <!-- 下单时间 -->
      <el-table-column label="下单时间" width="150">
        <template slot-scope="scope">
          {{ scope.row.orderTime}}
        </template>
      </el-table-column>
      <!-- 手机号 -->
      <el-table-column prop="phone" label="手机号" width="150">
      </el-table-column>
      <!-- 收货人 -->
      <el-table-column prop="consignee" label="收货人" width="150">
      </el-table-column>
      <!-- 配送地址 -->
      <el-table-column prop="deliverAddress" label="配送地址" width="150">
      </el-table-column>
      <!-- 送达时间 -->
      <el-table-column label="送达时间" width="150">
        <template slot-scope="scope">
          {{ scope.row.deliveryTime }}
        </template>
      </el-table-column>
      <!-- 用户备注 -->
      <el-table-column prop="remarks" label="用户备注" width="150">
      </el-table-column>
      <!-- 订单金额 -->
      <el-table-column prop="orderAmount" label="订单金额" width="150">
      </el-table-column>
      <!-- 订单状态 -->
      <el-table-column prop="orderState" label="订单状态" width="150">
      </el-table-column>
      <!-- 操作 -->
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >查看</el-button
          >
          <el-button type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="mt20">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { reqOrderList } from "@/api/order";
// 原生处理时间格式
import {parseTime} from "@/utils/date";
// 处理时间格式插件
import * as dayjs from 'dayjs'

export default {
  data() {
    return {
      searchData: {
        orderNo: "",
        phone: "",
        consignee: "",
        orderState: "",
        date:[]
      },
      tableData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1518 弄",
          zip: 200333,
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1517 弄",
          zip: 200333,
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1519 弄",
          zip: 200333,
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          province: "上海",
          city: "普陀区",
          address: "上海市普陀区金沙江路 1516 弄",
          zip: 200333,
        },
      ],
      w: document.body.clientWidth - 220,
      //分页
      currentPage: 1, //当前页码值,一定要以1开始
      pageSize: 10, //每一页显示多少条
      total: 0, //总共条数
    };
  },
  methods: {
    //获取订单数据
    async getData() {
        console.log(this.searchData.date);
      //2.发送ajax
      let res = await reqOrderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchData,
        date:this.searchData.date==null?JSON.stringify([]):JSON.stringify(this.searchData.date)

      });

      //3.解构
      console.log('====>res',res);
      let { data, total } = res.data;

      //判定---如果把当前页数据清空后,需要回拉一页,显示前一页的数据,如果已经是第一页,不操作
      if (!data.length && this.currentPage != 1) {
        this.currentPage -= 1; //回拉一页
        this.getData(); //重新渲染数据
      }

      //4.处理数据格式
      console.log(data);
      data.map(v=>{
        // v.deliveryTime=parseTime(v.deliveryTime)
        // v.orderTime=parseTime(v.orderTime)
        v.deliveryTime=dayjs(v.deliveryTime).format("YYYY-MM-DD HH:mm:ss")
        v.orderTime=dayjs(v.orderTime).format("YYYY-MM-DD HH:mm:ss")
      })
      //5.把处理好的数据放到指定位置
      this.tableData = data;
      this.total = total;
    },
    //查询
    onSearch() {
        this.currentPage=1;
        this.getData();
    },
    handleClick(row) {
      console.log(row);
    },
    //动态计算宽度
    resizeWidth() {
      // console.log(document.body.clientWidth);
      //把当前屏幕宽度-左菜单和margin的距离= 右边的宽度
      this.w = document.body.clientWidth - 220;
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
  },
  //监听width的变化
  created() {
    //进入页面获取订单数据
    this.getData();
    //添加window调整窗口大小的事件
    window.addEventListener("resize", this.resizeWidth);
  },
  //销毁监听事件
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeWidth);
  },
};
</script>

<style lang="less" scoped>
.el-form {
  padding: 20px;
  background-color: #fff;
}
.mt20 {
  margin-top: 20px;
}
.el-pagination{
  background-color: #fff;
}
.box{
  background-color: #fff;
}
</style>